package com.jubao.ejb.dao.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.jubao.ejb.dao.StoreDaoLocal;
import com.jubao.ejb.dao.StoreDaoRemote;
import com.jubao.ejb.dao.util.DaoConstant;
import com.jubao.ejb.entity.Store;

@Stateless
@LocalBinding(jndiBinding = DaoConstant.StoreDaoLocalJndiName)
@RemoteBinding(jndiBinding = DaoConstant.StoreDaoRemoteJndiName)
public class StoreDaoImpl implements StoreDaoLocal, StoreDaoRemote {
	static Logger logger = Logger.getLogger(StoreDaoImpl.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = -4283402492463468903L;

	@PersistenceContext(unitName = "default")
	protected EntityManager em;

	public Store find(long sysId) {
		Store result = em.find(Store.class, sysId);
		return result;
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.dao.StoreDao#find(java.lang.String)
	 */
	public Store find(String id) {
		Query q = em.createNamedQuery("StoreDao.eql.findStoreById").setParameter("id", id).setMaxResults(1)
				.setHint("org.hibernate.cacheable", new Boolean(true));
		//.setHint("org.hibernate.cacheRegion", "/entity/Category/Single")
		List<Store> stores = q.getResultList();
		Store result = null;
		if (stores != null && stores.size() > 0) {
			result = stores.get(0);
		}
		return result;
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public Store save(Store store) {
		Store c = em.merge(store);
		return c;
	}

	public void deleteSiteBySiteId(String siteId) {
	}

	

}
